package net.mof.manual.meta

import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName
import net.mof.manual.MarkedClassInfo
import net.mof.manual.Utils.jsonObject
import net.mof.manual.Utils.upFirstChar
import javax.lang.model.type.TypeKind

/**
 *
 * 基本数据类型
 *
 * Created by LiYueHui on 2020/2/2.
 */
class PrimitiveFieldMetaInfo : BaseFieldMetaInfo() {
    override fun optField(): String {
        when (element?.asType()?.kind) {
            TypeKind.INT, TypeKind.SHORT -> return "Int"
            TypeKind.LONG -> return "Long"
            TypeKind.DOUBLE, TypeKind.FLOAT -> return "Double"
            TypeKind.BOOLEAN -> return "Boolean"
        }
        return "Int"
    }

    override fun instanceTypeName(): TypeName {
        when (element?.asType()?.kind) {
            TypeKind.INT -> return ClassName.INT
            TypeKind.SHORT -> return ClassName.SHORT
            TypeKind.LONG -> return ClassName.LONG
            TypeKind.DOUBLE -> return ClassName.DOUBLE
            TypeKind.FLOAT -> return ClassName.FLOAT
            TypeKind.BOOLEAN -> return ClassName.BOOLEAN
        }
        return ClassName.BOOLEAN
    }

    override fun jsonToTypeGet(builder: CodeBlock.Builder, markedClassInfo: MarkedClassInfo) {
        if (element!!.asType().kind == TypeKind.BOOLEAN && filedName().startsWith("is")) {
            builder.addStatement("\$L.set\$L(\$L)", markedClassInfo.getFileName()
                    ?.toLowerCase(), upFirstChar(filedName().subSequence(2, filedName().length).toString()), value);
        } else {
            builder.addStatement("\$L.set\$L(\$L)", markedClassInfo.getFileName()
                    ?.toLowerCase(), upFirstChar(filedName()), value);
        }
    }

    override fun typeToJsonObjectPut(builder: CodeBlock.Builder?, param: String?) {
        val method: String
        if (element!!.asType().kind == TypeKind.BOOLEAN) {
            if (filedName().startsWith("is")) {
                method = filedName() + "()"
            } else {
                method = "is" + upFirstChar(filedName()).toString() + "()";
            }
        } else {

            if (filedName().startsWith("is")) {
                method = filedName() + "()"
            } else {
                method = "get" + upFirstChar(filedName()).toString() + "()"
            }

        }
        builder?.addStatement("\$L.put(\$S,\$L.\$L)", jsonObject, value, param,
                method)
    }
}